<template>
  <el-card class="box_card">
    <template #header>
      <div class="manage_list_query">
        <el-form
          :inline="true"
          label-position="left"
          :model="articleListQuery"
          class="demo-form-inline"
        >
          <el-form-item label="文章标题：">
            <el-input
              v-model="articleListQuery.articleTitle"
              clearable
              placeholder="文章标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="文章分类：">
            <el-select
              clearable
              v-model="articleListQuery.articleClass"
              placeholder="请选择文章分类"
            >
              <el-option
                v-for="item in articleType"
                :label="item.typeName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="文章标签：">
            <el-input
              v-model="articleListQuery.label"
              clearable
              placeholder="文章标签"
            ></el-input>
          </el-form-item>
          <el-form-item label="发布状态：">
            <el-select
              clearable
              v-model="articleListQuery.articleStatus"
              placeholder="请选择发布状态"
            >
              <el-option
                v-for="item in statusOptin"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitQuery">查询</el-button>
            <el-button type="primary" @click="addArticle">
              <span class="iconify" data-icon="subway:add-1" data-width="10">
              </span>
              添加文章
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table
      :data="articleList"
      style="width: 100%"
      row-key="id"
      :header-cell-style="{
        'text-align': 'center',
      }"
      :cell-style="{
        'text-align': 'center',
      }"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="articleTitle" label="文章名称" width="220" />

      <el-table-column label="文章分类">
        <template v-slot:default="scope">
          {{
            articleType.find((item) => item.id == scope.row.articleClass)
              ?.typeName
          }}
        </template>
      </el-table-column>
      <el-table-column prop="articleLabels" label="文章标签" />

      <el-table-column label="文章状态">
        <template v-slot:default="scope">
          <el-tag v-if="scope.row.articleStatus == 1">草稿</el-tag>
          <el-tag type="success" v-else-if="scope.row.articleStatus == 2">
            发布
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="文章封面">
        <template v-slot:default="scope">
          <el-image
            fit="scale-down"
            class="article_cover"
            :src="scope.row.articleImg"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160" fixed="right">
        <template v-slot:default="scope">
          <el-button
            size="small"
            type="primary"
            @click="modifyArticle(scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="deleteArticle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script lang="ts" setup>
// vue 框架插件
import { onMounted, ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
// api 接口
import {
  getArticleListApi,
  deleteArticleApi,
  getArticleTypeListApi,
} from '@/api/article'
// 组件
import { ElMessage } from 'element-plus'
const router = useRouter()
// 文章发布时的状态
let statusOptin = ref([
  { label: '草稿', value: 1 },
  {
    label: '发布',
    value: 2,
  },
])
// 文章分类列表
const articleType = ref<any[]>([])
// * 文章列表
const articleList = ref([])
// ? 点击根据条件查询文章列表
onMounted(async () => {
  await getArticleType()
  await getArticleList()
})

const articleListQuery = reactive({
  articleTitle: '', // 文章标题
  label: '', // 文章标签
  articleClass: '', // 文章分类
  articleStatus: '', // 文章状态
})

const submitQuery = () => {
  getArticleList()
}
const addArticle = () => {
  router.push('/article/add')
}

// ? 跳转到修改文章页面
const modifyArticle = (id: string) => {
  router.push({
    path: '/article/modify',
    query: {
      id,
    },
  })
}

// ? 删除文章的方法
const deleteArticle = async (id: string) => {
  let res: any = await deleteArticleApi(id)
  console.log(res)
  if (res.status === 0) {
    ElMessage.success('删除文章成功')
    getArticleList()
  }
}
/**
 * 获取文章列表的方法
 */
const getArticleList = async () => {
  var res: any = await getArticleListApi(articleListQuery)
  if (res.status === 0) {
    articleList.value = res.data
  }
}
// 获取文章分类
const getArticleType = async () => {
  let res: any = await getArticleTypeListApi()
  if (res.status == 0) {
    articleType.value = res.data
  }
}
</script>
<style lang="scss" scoped>
:v-deep(.el-card__header) {
  padding-bottom: 0;
}

.box_card {
  margin: $mainMargin;
  .article_cover {
    width: 100px;
    height: 100px;
  }
}
</style>
